﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;

namespace NutriLeaf.Phone.Pages.Articles
{
    public partial class Articles : PhoneApplicationPage,INotifyPropertyChanged
    {
        ArticleSync.ArticleOfflineContext context = new ArticleSync.ArticleOfflineContext(Constants.SyncCache.Articles, new Uri(Constants.SyncServices.Article));

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
        #endregion

        private List<ArticleSync.ArticleCategories> articleCategories;


        public List<ArticleSync.ArticleCategories> ArticleCategory
        {
            get
            {
                return articleCategories;
            }
            set
            {
                articleCategories = value;
                NotifyPropertyChanged("ArticleCategories");
            }
        }

        public Articles()
        {
            InitializeComponent();
            
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            context.LoadCompleted += new EventHandler<Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs>(context_LoadCompleted);
            context.LoadAsync();

        }

        void context_LoadCompleted(object sender, Microsoft.Synchronization.ClientServices.IsolatedStorage.LoadCompletedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
               {
                   SelectCategory(0);
               });
        }

        public void SelectCategory(int categoryId)
        {
            Dispatcher.BeginInvoke(() =>
               {

                   if (categoryId == 0)
                   {
                       ArticleCategory = context.ArticleCategoriesCollection.Where(o => o.ParentCategoryID == null).ToList();
                       ArticleCategoriesLB.ItemsSource = ArticleCategory;
                       CurrentCategoryLBL.Text = "";
                   }
                   else
                   {
                       ArticleSync.ArticleCategories cat = context.ArticleCategoriesCollection.Where(o => o.ArticleCategoryID == categoryId).SingleOrDefault();
                       if (cat == null)
                       {
                           CurrentCategoryLBL.Text = "";
                       }
                       else
                       {
                           CurrentCategoryLBL.Text = cat.CategoryName;
                       }
                       ArticleCategory = context.ArticleCategoriesCollection.Where(o => o.ParentCategoryID == categoryId).ToList();
                       ArticleCategoriesLB.ItemsSource = ArticleCategory;
                   }
               });
        }

        private void ArticleCategoriesLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ArticleCategoriesLB.SelectedItem != null)
            {
                ArticleSync.ArticleCategories cat = (ArticleSync.ArticleCategories)ArticleCategoriesLB.SelectedItem;
                SelectCategory(cat.ArticleCategoryID);
            }
        }

        private void CurrentCategoryLBL_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if(CurrentCategoryLBL.Text!=String.Empty)
            {
                string parentCategoryName = CurrentCategoryLBL.Text;
                ArticleSync.ArticleCategories cat = context.ArticleCategoriesCollection.Where(o => o.CategoryName == parentCategoryName).SingleOrDefault();
                if (cat == null)
                {
                    SelectCategory(0);
                }
                else
                {
                    if (cat.ParentCategoryID == null)
                    {
                        SelectCategory(0);
                    }
                    else
                    {
                        SelectCategory((int)cat.ParentCategoryID);
                    }
                }

                
            }

        }



        
    }
}